<?php
use Workerman\Worker;


// 使用websocket协议监听6161端口
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('websocket://0.0.0.0:6161');

//  当浏览器(包括用户手机浏览器和电脑浏览器)发来消息时的处理逻辑
$worker->onMessage = function(TcpConnection $connection, $data) {
    // 这个静态变量用来存储电脑浏览器的websocket连接，方便推送使用
    static $daping_connection = null;
    if ($data == 'ping'){
        $connection->send('pong');
    }else{
        $rec = json_decode($data);
        if ($rec['author'] == "go"){
            $return  = [
                "message"=>"你是 go，我知道了"
            ];
            $connection->send( json_encode($return));
        }
    }


};
Worker::runAll();